package com.demo.caizi.aop;

import com.demo.caizi.biz.domain.CustomInfo;
import com.demo.caizi.biz.domain.TradeInfo;
import com.demo.caizi.config.context.CustomContextHolder;
import com.demo.caizi.telemetry.MetricsLogPOJO;
import com.demo.caizi.telemetry.TelemetryLog;
import com.demo.caizi.telemetry.TradeInfoMetrics;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.api.PodamFactoryImpl;

import java.util.Date;

@Aspect
@Component
public class MetricsActionAop {

//    @Pointcut("@annotation(MetricsAction)")
//    public void pointcut(MetricsAction metricsAction){
//
//    }
    @Autowired
    PodamFactory podamFactory;

    @Around("@annotation(metricsAction)")
    public Object around(ProceedingJoinPoint joinPoint, MetricsAction metricsAction) throws Throwable {
        MetricsLogPOJO metricsLogPOJO = new MetricsLogPOJO();
        CustomInfo customInfo = CustomContextHolder.getHolder().getCustomInfo();

        BeanUtils.copyProperties(customInfo,metricsLogPOJO);
        metricsLogPOJO.setMetricsName(metricsAction.metricsName());
        metricsLogPOJO.setFeatureName(metricsAction.featureName());
        metricsLogPOJO.setVerb(metricsAction.verb());
        metricsLogPOJO.setDate(new Date());


        TelemetryLog.log(metricsLogPOJO);

        return joinPoint.proceed();
    }


}
